//
//  AppDelegate.swift
//  MyWeiBo
//
//  Created by mac on 15/3/2.
//  Copyright (c) 2015年 gpr. All rights reserved.
//

import UIKit
//import JSONModel

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        JSONModelSerializer.shareInstance.nameSpace = WB_NAMESPACE
        let win = UIWindow(frame: UIScreen.mainScreen().bounds)
        var rootVC: UIViewController? = nil
        if AccountTool.getAccountFromLoacl() != nil {
            rootVC = WBTabBarViewController.WBMaiViewController()
        } else {
            rootVC = OAuthController.oAuthController()
        }
        win.rootViewController = rootVC
        win.makeKeyAndVisible()
        window = win
        setUpAppaperance()
        addNotificationObserver()
        return true
    }
    
    override func finalize() {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: WB_SwithcToHomeController_Notification, object: nil)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: WB_SwithcToOAuthController_Notification, object: nil)
    }
    
    // MARK: 添加通知监听
    func addNotificationObserver() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchToTabBarViewController", name: WB_SwithcToHomeController_Notification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchToOAuthController", name: WB_SwithcToOAuthController_Notification, object: nil)
    }
    
    // MARK: 跳转到主页控制器
    func switchToTabBarViewController() {
        let tabBarVC = WBTabBarViewController.WBMaiViewController()
        self.window?.rootViewController = tabBarVC
    }
    
    // MARK: 跳转到登陆页面
    func switchToOAuthController() {
        let oauthVC = OAuthController.oAuthController()
        self.window?.rootViewController = oauthVC
    }

    ///  设置应用程序的全局外观
    func setUpAppaperance() {
        var titleAttr = [NSForegroundColorAttributeName: UIColor.orangeColor()]
        let tabBarItem = UITabBarItem.appearance()
        tabBarItem.setTitleTextAttributes(titleAttr, forState: UIControlState.Selected)
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }

    func applicationWillTerminate(application: UIApplication) {

    }


}

